package com.wmh.baseservice.robot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.robot.convert.RobotTaskStatementV2Convert;
import com.wmh.baseservice.robot.entity.RobotInfo;
import com.wmh.baseservice.robot.entity.RobotTaskStatementV2;
import com.wmh.baseservice.robot.mapper.RobotTaskStatementV2Mapper;
import com.wmh.baseservice.robot.pojo.dto.TaskStatementFileReps;
import com.wmh.baseservice.robot.pojo.dto.robottaskadpushunit.RobotTaskAdPushUnitPushReqVO;
import com.wmh.baseservice.robot.pojo.vo.TaskStatementAppendResultReqVO;
import com.wmh.baseservice.robot.service.RobotTaskStatementV2Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Base64;

/**
 * @author 谢骏
 * @description 针对表【robot_task_statement_v2(机器人任务报表 - v2 (发布任务单元表))】的数据库操作Service实现
 * @createDate 2023-09-11 14:14:09
 */
@Service
public class RobotTaskStatementV2ServiceImpl extends ServiceImpl<RobotTaskStatementV2Mapper, RobotTaskStatementV2>
        implements RobotTaskStatementV2Service {

    @Resource
    private UserUtil userUtil;

    @Override
    public void create(RobotTaskAdPushUnitPushReqVO reqVO, RobotInfo robotInfo) {
        saveBatch(RobotTaskStatementV2Convert.INSTANCE.convertList(reqVO, robotInfo));
    }

    @Override
    public void appendResult(TaskStatementAppendResultReqVO reqVO) {
        checkExist(reqVO.getStatementId());
        updateById(RobotTaskStatementV2Convert.INSTANCE.convert(reqVO));
    }

    @Override
    public TaskStatementFileReps getTaskStatementFile(Long id) {
        RobotTaskStatementV2 entity = checkExist(id);
        checkDwPermission(entity);
        if (StrUtil.isBlank(entity.getTaskFileBase64()) || StrUtil.isBlank(entity.getTaskFileSuffix())) {
            throw new ServiceException("该报表不存在附件或者附件扩展名");
        }
        return new TaskStatementFileReps()
                .setContent(Base64.getDecoder().decode(entity.getTaskFileBase64()))
                .setFileFullName(entity.getTaskName() + "." + entity.getTaskFileSuffix());
    }

    @Override
    public TaskStatementFileReps getTaskStatementLog(Long id) {
        RobotTaskStatementV2 entity = checkExist(id);
        checkDwPermission(entity);
        if (StrUtil.isBlank(entity.getTaskLogBase64())) {
            throw new ServiceException("该报表不存在日志文件");
        }
        return new TaskStatementFileReps().setContent(Base64.getDecoder().decode(entity.getTaskLogBase64()))
                .setFileFullName(entity.getTaskName() + ".log");
    }

    private RobotTaskStatementV2 checkExist(Long id) {
        RobotTaskStatementV2 entity = getById(id);
        if (entity == null) {
            throw new ServiceException("该报表已经不存在");
        }
        return entity;
    }

    private void checkDwPermission(RobotTaskStatementV2 entity) {
        if (!userUtil.isSuperAdmin() && !entity.getAdId().equals(userUtil.getId())) {
            throw new ServiceException("您无权下载他人文件");
        }
    }

}




